| Conditions | 1 |
| Paths | 1 |
| Total Lines | 70 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | define(['polyglot', 'moment', 'helper'], function (Polyglot, moment, helper) { |
||
| 3 | return function (config) { |
||
| 4 | var router; |
||
| 5 | |||
| 6 | function languageSelect(el) { |
||
| 7 | var select = document.createElement('select'); |
||
| 8 | select.className = 'language-switch'; |
||
| 9 | select.addEventListener('change', setSelectLocale); |
||
| 10 | el.appendChild(select); |
||
| 11 | |||
| 12 | // Keep english |
||
| 13 | select.innerHTML = '<option>Language</option>'; |
||
| 14 | for (var i = 0; i < config.supportedLocale.length; i++) { |
||
| 15 | select.innerHTML += '<option value="' + config.supportedLocale[i] + '">' + config.supportedLocale[i] + '</option>'; |
||
| 16 | } |
||
| 17 | } |
||
| 18 | |||
| 19 | function setSelectLocale(event) { |
||
| 20 | router.fullUrl({ lang: event.target.value }, false, true); |
||
| 21 | } |
||
| 22 | |||
| 23 | function setLocale(lang) { |
||
| 24 | localStorage.setItem('language', getLocale(lang)); |
||
| 25 | location.reload(); |
||
| 26 | } |
||
| 27 | |||
| 28 | function getLocale(input) { |
||
| 29 | var language = input || localStorage.getItem('language') || navigator.languages && navigator.languages[0] || navigator.language || navigator.userLanguage; |
||
| 30 | var locale = config.supportedLocale[0]; |
||
| 31 | config.supportedLocale.some(function (item) { |
||
| 32 | if (language.indexOf(item) !== -1) { |
||
| 33 | locale = item; |
||
| 34 | return true; |
||
| 35 | } |
||
| 36 | return false; |
||
| 37 | }); |
||
| 38 | return locale; |
||
| 39 | } |
||
| 40 | |||
| 41 | function setTranslation(json) { |
||
| 42 | _.extend(json); |
||
| 43 | |||
| 44 | if (moment.locale(_.locale()) !== _.locale()) { |
||
| 45 | moment.defineLocale(_.locale(), { |
||
| 46 | longDateFormat: { |
||
| 47 | LT: 'HH:mm', |
||
| 48 | LTS: 'HH:mm:ss', |
||
| 49 | L: 'DD.MM.YYYY', |
||
| 50 | LL: 'D. MMMM YYYY', |
||
| 51 | LLL: 'D. MMMM YYYY HH:mm', |
||
| 52 | LLLL: 'dddd, D. MMMM YYYY HH:mm' |
||
| 53 | }, |
||
| 54 | calendar: json.momentjs.calendar, |
||
| 55 | relativeTime: json.momentjs.relativeTime |
||
| 56 | }); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | function init(r) { |
||
| 61 | router = r; |
||
| 62 | window._ = new Polyglot({ locale: getLocale(router.getLang()), allowMissing: true }); |
||
| 63 | helper.getJSON('locale/' + _.locale() + '.json?' + config.cacheBreaker).then(setTranslation); |
||
| 64 | } |
||
| 65 | |||
| 66 | return { |
||
| 67 | init: init, |
||
| 68 | getLocale: getLocale, |
||
| 69 | setLocale: setLocale, |
||
| 70 | languageSelect: languageSelect |
||
| 71 | }; |
||
| 72 | }; |
||
| 73 | }); |
||
| 74 |